package com.shop.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.pojo.Auctionproduct;
import com.shop.pojo.AuctionproductExample;
import com.shop.pojo.Auctionrecord;
import com.shop.pojo.Auctionuser;
import com.shop.pojo.CustomAuctionproduct;
import com.shop.service.AuctionService;
import com.shop.tools.CustomException;

@Controller
public class AuctionController {

	// 每页记录数
	private static final int PAGE_SIZE = 6;
	@Autowired
	private AuctionService auctionService;

	@RequestMapping(value = "/findAllAuctions")
	public ModelAndView findAllAuctions(@ModelAttribute("condition") Auctionproduct auction,
			@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum) {
		System.out.println("AuctionController.findAllAuctions()");

		ModelAndView mv = new ModelAndView();
		// 设置分页区间
		PageHelper.startPage(pageNum, PAGE_SIZE);
		List<Auctionproduct> auctions = auctionService.findAllAuctions(auction);
		// 创建分页bean
		PageInfo page = new PageInfo<>(auctions);
		mv.addObject("page", page);
		mv.addObject("auctions", auctions);
		mv.setViewName("index");
		return mv;
	}

	// 竞拍品详情页
	@RequestMapping("/getAuctionInfo/{auctionId}")
	public ModelAndView getAuctionInfo(@PathVariable Integer auctionId) {
		Auctionproduct auctionDetail = auctionService.selectAuctionInfo(auctionId);
		ModelAndView mv = new ModelAndView();
		mv.addObject("auctionDetail", auctionDetail);
		mv.setViewName("auctionDetail");
		return mv;
	}

	// 跳转到添加拍卖品页面
	@RequestMapping("toAddAuction")
	public String toAddAuction() {
		return "addAuction";
	}

	// 添加拍卖品
	@RequestMapping("addAuction")
	public String addAuction(MultipartFile filename, Auctionproduct auction) throws IllegalStateException, IOException {
		// 判断是否有重新上传图片
		if (filename.getSize() > 0) {
			// 原文件名
			// String oldFilename= filename.getOriginalFilename();

			// 设置拍卖品的auctionpic
			// 使用UUID解决图片不重名
			String auctionpic = UUID.randomUUID().toString().replaceAll("-", "");
			// 使用FilenameUtils工具类（需引入commons-io的依赖）获取文件的后缀
			String extension = FilenameUtils.getExtension(filename.getOriginalFilename());
			auction.setAuctionpic(auctionpic + "." + extension);
			filename.transferTo(new File("D:\\feiq\\Recv Files\\images\\" + auctionpic + "." + extension));

			// 设置拍卖品图片的MIME类型
			auction.setAuctionpictype(filename.getContentType());
		}
		int row = auctionService.insertSelective(auction);
		return "redirect:findAllAuctions";
	}

	// 跳转到修改拍卖品页面
	@RequestMapping("/toEditAuction")
	public ModelAndView toEditAuction(Integer id) {
		ModelAndView mv = new ModelAndView();
		Auctionproduct auctionproduct = auctionService.selectAuctionById(id);
		System.out.println(auctionproduct);
		mv.addObject("auction", auctionproduct);
		mv.setViewName("editAuction");
		return mv;
	}

	// 修改拍卖品
	@RequestMapping("/updateAuction")
	public String updateAuction(MultipartFile filename, Auctionproduct auction)
			throws IllegalStateException, IOException {
		// 判断是否有重新上传图片
		if (filename.getSize() > 0) {
			// 设置拍卖品的auctionpic
			// 使用UUID解决图片不重名
			String auctionpic = UUID.randomUUID().toString().replaceAll("-", "");
			// 使用FilenameUtils工具类（需引入commons-io的依赖）获取文件的后缀
			String extension = FilenameUtils.getExtension(filename.getOriginalFilename());
			auction.setAuctionpic(auctionpic + "." + extension);
			filename.transferTo(new File("D:\\feiq\\Recv Files\\images\\" + auctionpic + "." + extension));
			// 设置拍卖品图片的MIME类型
			auction.setAuctionpictype(filename.getContentType());
		}
		int row = auctionService.updateAuction(auction);
		return "redirect:findAllAuctions";
	}

	// 竞拍
	@RequestMapping("/saveAuctionRecord")
	public String saveAuctionRecord(Auctionrecord record, HttpSession session) throws CustomException {
		// 封装用户id和竞拍时间即当前系统时间
		Auctionuser user = (Auctionuser) session.getAttribute("user");
		record.setUserid(user.getUserid());
		record.setAuctiontime(new Date());
		auctionService.insertAuctionRecord(record);
		return "redirect:/getAuctionInfo/" + record.getAuctionid();
	}

	// 查看竞拍结果
	@RequestMapping("getAuctionResult")
	public ModelAndView getAuctionResult() {
		ModelAndView mv = new ModelAndView();
		List<CustomAuctionproduct> auctionEnd = auctionService.listAuctionEnd();
		List<Auctionproduct> auctionNotEnd = auctionService.listAuctionNotEnd();
		mv.addObject("auctionEndList", auctionEnd);
		mv.addObject("auctionNotEndList", auctionNotEnd);
		mv.setViewName("auctionResult");
		return mv;
	}
	
	//删除竞拍品
	@RequestMapping("/deleteAuction")
	public String deleteAuction(Integer id) {
		auctionService.deleteAuction(id);
		return"redirect:/findAllAuctions";
	}
}
